//
//  ZYCTipView.m
//  ZDSC
//
//  Created by ZDTX on 2021/10/11.
//

#import "ZYCTipView.h"
@interface ZYCTipView ()

@property (nonatomic,strong)UIView *baseView;
@property (nonatomic,strong)UIView *dimView;
@property (nonatomic, strong) UILabel *tipLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *comfirmButton;

@end
@implementation ZYCTipView

- (void)setTitle:(NSString *)title{
    _title = title;
    _tipLabel.text = title;
}

- (void)setContent:(NSString *)content{
    _content = content;
    _contentLabel.text = content;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(0, 0, kScreenWidth,  kScreenHeight);
        self.hidden = NO;
        self.dimView.hidden = NO;
        self.baseView.hidden = NO;
        [self setupUI];

    }
    return self;
}

- (void)setupUI{
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.tipLabel];
    [self.baseView addSubview:self.contentLabel];
    [self.baseView addSubview:self.cancelButton];
    [self.baseView addSubview:self.comfirmButton];
    
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(280.0f, 155.0f));
    }];
    
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.baseView);
        make.top.equalTo(self.baseView).offset(22.0f);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.baseView);
        make.top.equalTo(self.tipLabel.mas_bottom).offset(20.0f);
    }];
    
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.baseView.mas_centerX).offset(-20.0f);
        make.top.equalTo(self.contentLabel.mas_bottom).offset(20.0f);
        make.size.mas_equalTo(CGSizeMake(100.0f, 40.0f));
    }];
    
    [self.comfirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView.mas_centerX).offset(20.0f);
        make.top.equalTo(self.contentLabel.mas_bottom).offset(20.0f);
        make.size.mas_equalTo(CGSizeMake(100.0f, 40.0f));
    }];
    
}



- (UIView *)dimView{
    if (!_dimView) {
        _dimView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,  [UIScreen mainScreen].bounds.size.height)];
        _dimView.backgroundColor = [UIColor blackColor];
        _dimView.alpha = 0.7;
        [self addSubview:_dimView];
    }
    return _dimView;
}


- (UIView *)baseView {
    if (!_baseView) {
        _baseView = [[UIView alloc] init];
        _baseView.clipsToBounds = YES;
        _baseView.layer.cornerRadius = 6.0f;
        _baseView.backgroundColor = HEX_RGB(@"FFFFFF");

    }
    return _baseView;
}


- (UILabel *)tipLabel {
    if (!_tipLabel) {
        _tipLabel = [UILabel LabelWithFont:kSystemBoldFont(18) titleColor:HEX_RGB(@"#2B302D") textAlignment:ZZTextAlignmentCenter];
    }
    return _tipLabel;
}

- (UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [UILabel LabelWithFont:kCustomFont(@"PingFangSC-Regular", 14) titleColor:HEX_RGB(@"#999999") textAlignment:ZZTextAlignmentCenter];
    }
    return _contentLabel;
}


- (UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelButton.frame = CGRectMake(0,0, 100, 40);
        _cancelButton.tag = 1000;
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:HEX_RGB(kTitleWhiteColor) forState:UIControlStateNormal];
//        _cancelButton.backgroundColor = HEX_RGB(@"#41B166");
        _cancelButton.titleLabel.font = kSystemBoldFont(16.0f);
        [_cancelButton addTarget:self action:@selector(clickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        _cancelButton.layer.cornerRadius = 20;
        _cancelButton.layer.masksToBounds = YES;
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1, 0);
        gradientLayer.colors = @[(__bridge id)HEX_RGB(@"#F6EEB0").CGColor,
                                 (__bridge id)HEX_RGB(@"#009B4C").CGColor];
        gradientLayer.locations = @[@(0.3),@(1.0f)];
        gradientLayer.frame = _cancelButton.bounds;
        gradientLayer.cornerRadius = 20;
        [_cancelButton.layer insertSublayer:gradientLayer atIndex:0];
    }
    return _cancelButton;
}

- (UIButton *)comfirmButton {
    if (!_comfirmButton) {
        _comfirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _comfirmButton.frame = CGRectMake(0,0, 100, 40);
        _comfirmButton.tag = 1001;
        [_comfirmButton setTitle:@"确认" forState:UIControlStateNormal];
        [_comfirmButton setTitleColor:HEX_RGB(kTitleWhiteColor) forState:UIControlStateNormal];
//        _cancelButton.backgroundColor = HEX_RGB(@"#41B166");
        _comfirmButton.titleLabel.font = kSystemBoldFont(16.0f);
        [_comfirmButton addTarget:self action:@selector(clickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        _comfirmButton.layer.cornerRadius = 20;
        _comfirmButton.layer.masksToBounds = YES;
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1, 0);
        gradientLayer.colors = @[(__bridge id)HEX_RGB(@"#F6EEB0").CGColor,
                                 (__bridge id)HEX_RGB(@"#009B4C").CGColor];
        gradientLayer.locations = @[@(0.3),@(1.0f)];
        gradientLayer.frame = _comfirmButton.bounds;
        gradientLayer.cornerRadius = 20;
        [_comfirmButton.layer insertSublayer:gradientLayer atIndex:0];
    }
    return _comfirmButton;
}

- (void)clickButtonAction:(UIButton *)sender{
    [self dismiss];
    if (sender.tag == 1001) {
        if (self.clickComfirmBlock) {
            self.clickComfirmBlock(self.filePath);
        }
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismiss];
}

- (void)show{
    self.hidden = NO;
}
- (void)dismiss
{
    self.hidden = YES;
}


@end
